#!/bin/sh
# Copyright (c) Nuvoton Technology Corp. All rights reserved.
# Description:	Ethernet/PPPoE startup script
# Version:	2013-04-15	first release version
#		W.C  Lin


if [ -z "$MSGTOTERMINAL" ]; then MSGTOTERMINAL="/dev/ttyS1"; fi

ETHERNET_FILE=/tmp/etc/ethernet_config
if [ ! -f $ETHERNET_FILE ]; then
        echo "Can't find $ETHERNET_FILE"
        exit 1
fi

# set DEVICE to wlan0
NET_DEVICE=`awk '{if ($1=="DEVICE") {print $2}}' $ETHERNET_FILE`
# set BOOTPROTO to DHCP/STATIC
NET_BOOTPROTO=`awk '{if ($1=="BOOTPROTO") {print $2}}' $ETHERNET_FILE`
# set IP address, only usful to STATIC IP
NET_IPADDR=`awk '{if ($1=="IPADDR") {print $2}}' $ETHERNET_FILE`
# set GATEWAY address, only usful to STATIC IP
NET_GATEWAY=`awk '{if ($1=="GATEWAY") {print $2}}' $ETHERNET_FILE`
# set MAC address, only usful to auto
NET_MACADDR=`awk '{if ($1=="MACADDR") {print $2}}' $ETHERNET_FILE`
# set USERNAME
PPPOE_USER=`awk '{if ($1=="USER") {print $2}}' $ETHERNET_FILE`
# set PASSWORD
PPPOE_PASSWD=`awk '{if ($1=="PASSWD") {print $2}}' $ETHERNET_FILE`


ConfigurationIPAddr()
{
	killall udhcpc
	if [ "$NET_BOOTPROTO" == "DHCP" ]; then
        	echo -e "Leasing an IP address ..." > $MSGTOTERMINAL
	        echo "udhcpc -i $NET_DEVICE"
			if udhcpc -i $NET_DEVICE -T 2 -A 2 -q -n > /dev/null; then
				echo -e "Got IP: \033[1;33m"`ifconfig $NET_DEVICE | grep inet | awk '{FS=":"} {print $2}' | sed 's/[^0-9\.]//g'`"\033[m" > $MSGTOTERMINAL        
				return 0
			fi
	fi

	echo "ifconfig $NET_DEVICE $IPADDR netmask 255.255.255.0"
	ifconfig $NET_DEVICE $NET_IPADDR netmask 255.255.255.0
	
	echo -e "My IP: \033[1;33m$NET_IPADDR\033[1;33m" > $MSGTOTERMINAL
	echo "route add default gw $NET_GATEWAY $NET_DEVICE"
	route add default gw $NET_GATEWAY $NET_DEVICE
	return 0
}

ConfigurationNetplug()
{
	if ! cat /etc/netplug/netplugd.conf | grep $NET_DEVICE; then
		echo $NET_DEVICE >> /etc/netplug/netplugd.conf
	fi

	if ! cat /etc/network/interfaces | grep $NET_DEVICE > /dev/null; then
		echo "auto  $NET_DEVICE" >> /etc/network/interfaces
	       	if [ "$NET_BOOTPROTO" == "DHCP" ]; then
			if udhcpc -i $NET_DEVICE -T 2 -A 2 -q -n > /dev/null; then
                    		echo "  iface $NET_DEVICE inet dhcp " >> /etc/network/interfaces
				return 0
			fi
		fi
	       	echo "  iface $NET_DEVICE inet static" 	>> /etc/network/interfaces
               	echo "  address $NET_IPADDR" 		>> /etc/network/interfaces         
        	echo "  netmask 255.255.255.0" 		>> /etc/network/interfaces       
		echo "  gateway $NET_GATEWAY" 		>> /etc/network/interfaces
	fi
	
	return 0
}

if [ "$NET_DEVICE" == "eth0" ]; then

	ifconfig eth0 up

	if [ -z $NET_MACADDR ]; then
		# Change ethernet mac address
		./macchanger -a -e $NET_DEVICE
		
		CURR_MACADDR=`./macchanger -s $NET_DEVICE | grep Current | awk '{ print $3 }'`

		if [ ! -z $CURR_MACADDR ] ; then
			sed -i "s|^MACADDR |MACADDR "$CURR_MACADDR"|g" "$ETHERNET_FILE"
			sync
		fi
	else
                # Change ethernet mac address
                if ./macchanger -m $NET_MACADDR $NET_DEVICE; then
			echo "MAC address of $NET_DEVICE is $NET_MACADDR."
		else
			# Change ethernet mac address
	                ./macchanger -a -e $NET_DEVICE
		fi
	fi

	if [ ! -d /usr/netplug ]; then
		ConfigurationIPAddr
	else
		ConfigurationNetplug
	fi

elif [ "$NET_DEVICE" == "ppp0" ]; then

	if [ ! -d /tmp/etc/ppp ] ; then mkdir -p /tmp/etc/ppp; fi

	./pppoe.sh start $PPPOE_USER $PPPOE_PASSWD &

	retry=0

        while [ $retry != 10 ]; do

           ifconfig ppp0 | grep "UP.*POINTOPOINT" > /dev/null

           if [ "$?" == "0" ]; then     break;  fi

           sleep 1

	   retry=`expr $retry + 1`

        done
        
	rm -f /etc/resolv.conf

        ln -s /etc/ppp/resolv.conf /etc/resolv.conf

fi

exit 0
